<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\TypeModel;
use Illuminate\Http\Request;

class TypesController extends Controller
{
    public function types(Request $request)
    {
        $types = TypeModel::get();
        return view("admin.types", compact("types"));
    }

    public function typesaddpage(Request $request)
    {
        $id = $request->input("id");
        $type = TypeModel::find($id);
//        $type = TypeModel::where("id",$id)->first();
        return view("admin.typesaddpage", compact("type"));
    }

    public function addInfo(Request $request)
    {

        $data = $request->except("_token");
        if ($data['id'] == 0) {
            TypeModel::create($data);
            $result = ["title"=>"添加数据成功！","err"=>0];
        } else {
            $type = TypeModel::find($data['id']);
            $type->update($data);
            $result = ["title"=>"数据编辑成功！","err"=>0];
        }
        $result = json_encode($result);
        return $result;
    }

    public function delete(Request $request)
    {
        $id = $request->input("id");
        TypeModel::where("id",$id)->delete();
        $result = ["title"=>"删除成功！","err"=>0];
        $result = json_encode($result);
        return $result;
    }
}
